﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Spinning.Spinning.Interface;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework;

namespace Spinning.Spinning.Entity
{
    public class ArrowButtonForStore : BaseEntity, ITouchable
    {
        public AnimationSprite animationSprite;
        bool _pressed;
        public delegate void TouchEventHandler(BaseEntity entity, TouchLocation touchLocation);
        public TouchEventHandler OnTouchEvent, OnReleaseEvent, OnHoldEvent;

        bool _isEnable;

        public bool isEnable
        {
            get
            {
                return _isEnable;
            }
            set 
            {
                _isEnable = value;
                if (animationSprite != null)
                {
                    if (isEnable) animationSprite.SetChangeFrameAnimation(new int[] { 0 });
                    else animationSprite.SetChangeFrameAnimation(new int[] { 1 });
                }
            }
        }

        public override void OnLoad()
        {
            animationSprite = new AnimationSprite();
            animationSprite.textureName = "item_store/arr";
            animationSprite.OnLoad();
            animationSprite.SetAnimation(new Point(2, 1));
            animationSprite.Position = new Vector2();
            this.Attach(animationSprite);
            isEnable = false;
        }

        public bool Contain(Microsoft.Xna.Framework.Vector2 position)
        {
            position = Vector2.Transform(position, UnProjection);
            Vector2 topLeft = Position - Origin * new Vector2(animationSprite._frameSize.X, animationSprite._frameSize.Y) * Scale;
            Vector2 rightDown = topLeft + new Vector2(animationSprite._frameSize.X, animationSprite._frameSize.Y) * Scale;
            return (position.X > topLeft.X && position.X < rightDown.X && position.Y > topLeft.Y && position.Y < rightDown.Y);
        }

        public void OnTouch(TouchLocation touchLocation)
        {
            _pressed = true;
            if (OnTouchEvent != null && isEnable)
                OnTouchEvent(this, touchLocation);
        }

        public void OnRelease(TouchLocation touchLocation)
        {
            _pressed = false;
            if (OnReleaseEvent != null && isEnable)
                OnReleaseEvent(this, touchLocation);
        }

        public void OnHold(TouchLocation touchLocation)
        {
            if (OnHoldEvent != null && isEnable)
                OnHoldEvent(this, touchLocation);
        }
    }
}
